package cn.anlab.anappframe.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.anlab.anappframe.R;

/**
 * Tab页
 * @author rongxinhua
 *
 */
public class CompTabGroup extends LinearLayout {
	
	private Context context;
	private OnTabClickListener onTabClickListener;
	private String[] tabTexts;
	private int defaultSelectedIndex;
	private LayoutInflater inflater;
	
	public CompTabGroup(Context context) {
		super(context);
	}

	public CompTabGroup(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		setOrientation(HORIZONTAL);
		setGravity(Gravity.CENTER);
		
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CompTabGroup);
        //Tab显示文本数组
        int tabTextsResId = a.getResourceId(R.styleable.CompTabGroup_tabTexts, 0);
        if(tabTextsResId != 0) {
        	tabTexts = getResources().getStringArray(tabTextsResId);        	
        }
        defaultSelectedIndex = a.getInt(R.styleable.CompTabGroup_defaultSelectedIndex, 0);
        
        //加载Tab布局
        for(int i = 0; i < tabTexts.length; i ++ ) {
        	LinearLayout.LayoutParams prm = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            View view = inflater.inflate(R.layout.comp_tabgroup_tab, null);
            
//            Drawable d = getResources().getDrawable(R.drawable.tab_bg);
            Drawable d = getResources().getDrawable(R.drawable.aaa);
            Bitmap bitmap = ((BitmapDrawable)d).getBitmap();  
            Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
            Canvas c = new Canvas(output);
            Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
            RectF rectF = new RectF(rect);
            Paint paint = new Paint();
            c.drawRoundRect(rectF, 0, 10, paint);
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            c.drawBitmap(bitmap, rect, rect, paint);
            
            BitmapDrawable bd = new BitmapDrawable(output);
            Drawable drawable = (Drawable)bd;
            view.setBackgroundDrawable(drawable);
            
//            /**
//             * 获得圆角图片
//             * @param bitmap
//             * @param roundPx
//             * @return
//             */
//            public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
//                Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
//                Canvas canvas = new Canvas(output);
//                final Paint paint = new Paint();
//                final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
//                final RectF rectF = new RectF(rect);
//                canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
//                paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
//                canvas.drawBitmap(bitmap, rect, rect, paint);
//                return output;
//            }
            
            
            TextView tv = (TextView) view.findViewById(R.id.textView1);
            tv.setText(tabTexts[i]);
            addView(view, prm);
        }
        
	}

	interface OnTabClickListener {
		
		public void onTabClick(View tab, int tabIndex);
		
	}
	
	
}
